#!/usr/bin/python
# -*- coding: utf8 -*-

import Tkinter
from sys import argv

set = {}

if len(argv) == 1:
	print "Bad parameters!\nUse:\n\tlister.py [option] filename\
	\nOptions:\n\t-c - crop comments\n\t-w - wrap lines"
	exit(1)
else:
	set["file_name"] = argv[-1]
	if len(argv) > 2:
		for param in argv[1:-1]:
			if param == "-c":
				set["crop_comments"] = True
			elif param == "-w":
				set["wrap"] = "word"
		
		if not set.has_key("crop_comments"):
			set["crop_comments"] = False
		elif not set.has_key("wrap"):
			set["wrap"] = "none"
	else:
		set["crop_comments"] = False
		set["wrap"] = "none"


file = open(set["file_name"], "r")
text = ""

try:
	for line in file:
		if set["crop_comments"]:
			stripped_line = line.strip()
			if not (stripped_line.startswith('#') or stripped_line.startswith(';') or stripped_line == ""):
				text += line.rstrip() + "\n"
		else:
			text += line.rstrip() + "\n"
finally:
	file.close()
	
#GUI
app = Tkinter.Tk()
app.title(set["file_name"])

quit_button = Tkinter.Button(app, text = "Quit", command = app.destroy)
quit_button.pack(side = "bottom", fill = "x")

scrollbar_x_frame = Tkinter.Frame(app)
scrollbar_x_frame.pack(side = "bottom", fill = "x")
scrollbar_x = Tkinter.Scrollbar(scrollbar_x_frame, orient = "horizontal", width = "0.3c", relief = "flat")
scrollbar_x_frame_placeholder = Tkinter.Frame(scrollbar_x_frame, width = "0.45c", relief = "ridge")
scrollbar_x_frame_placeholder.pack(side = "right", fill = "y")

scrollbar_y = Tkinter.Scrollbar(app, orient = "vertical", width = "0.3c", relief = "flat")

text_field = Tkinter.Text(app, yscrollcommand = scrollbar_y.set, xscrollcommand = scrollbar_x.set, width = 80, wrap = set["wrap"] , relief = "flat")
text_field.insert('insert', text)
#~ text_field.window_configure(text_field, state = "disabled")

scrollbar_x.config(command=text_field.xview)
scrollbar_y.config(command=text_field.yview)
scrollbar_x.pack(side = "bottom", fill = "x")
scrollbar_y.pack(side = "right", fill = "y")

text_field.pack()

app.mainloop()